package mailsystem.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import mailsystem.Connection;
import mailsystem.Telephone;

@SuppressWarnings("serial")
public class WindowInterface extends JFrame implements Telephone {

	/**
	 * 
	 */
	private JPanel contentPane;
	JButton btnButton1;
	JButton btnButton2;
	JButton btnButton3;
	JButton btnButton4;
	JButton btnButton5;
	JButton btnButton6;
	JButton btnButton7;
	JButton btnButton8;
	JButton btnButton9;
	JButton btnButton0;
	JButton btnAsterisk;
	JButton btnNumeral;
	JTextArea textOutput;
	JTextArea textAreaSpeaker;
	boolean send;
	private JPanel panel_2;
	private JTextArea textAreaDisplay;
	Connection connection;

	/**
	 * Create the frame.
	 */
	public WindowInterface() {
		setTitle("MailSystem");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 514, 495);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);

		textOutput = new JTextArea();
		textOutput.setEditable(false);
		textOutput.setRows(5);
		textOutput.setColumns(40);
		panel.add(textOutput);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(4, 3, 0, 0));

		btnButton1 = new JButton("1");
		btnButton1.addActionListener(new KeysListener());
		panel_1.add(btnButton1);

		btnButton2 = new JButton("2");
		btnButton2.addActionListener(new KeysListener());
		panel_1.add(btnButton2);

		btnButton3 = new JButton("3");
		btnButton3.addActionListener(new KeysListener());
		panel_1.add(btnButton3);

		btnButton4 = new JButton("4");
		btnButton4.addActionListener(new KeysListener());
		panel_1.add(btnButton4);

		btnButton5 = new JButton("5");
		btnButton5.addActionListener(new KeysListener());
		panel_1.add(btnButton5);

		btnButton6 = new JButton("6");
		btnButton6.addActionListener(new KeysListener());
		panel_1.add(btnButton6);

		btnButton7 = new JButton("7");
		btnButton7.addActionListener(new KeysListener());
		panel_1.add(btnButton7);

		btnButton8 = new JButton("8");
		btnButton8.addActionListener(new KeysListener());
		panel_1.add(btnButton8);

		btnButton9 = new JButton("9");
		btnButton9.addActionListener(new KeysListener());
		panel_1.add(btnButton9);

		btnAsterisk = new JButton("*");
		btnAsterisk.addActionListener(new KeysListener());
		panel_1.add(btnAsterisk);

		btnButton0 = new JButton("0");
		btnButton0.addActionListener(new KeysListener());
		panel_1.add(btnButton0);

		btnNumeral = new JButton("#");
		btnNumeral.addActionListener(new KeysListener());
		panel_1.add(btnNumeral);

		JPanel panel2 = new JPanel();
		contentPane.add(panel2, BorderLayout.SOUTH);

		textAreaSpeaker = new JTextArea();
		textAreaSpeaker.setRows(4);
		textAreaSpeaker.setColumns(25);
		panel2.add(textAreaSpeaker);

		JButton btnSend = new JButton("Enviar");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!textAreaSpeaker.getText().isEmpty())
				{
					connection.record(textAreaSpeaker.getText());
					textAreaSpeaker.setText("");
				}
			}
		});
		panel2.add(btnSend);

		JButton btnHungup = new JButton("Colgar");
		btnHungup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textAreaSpeaker.setText("");
				textOutput.setText("");
				connection.hangup();
			}
		});
		panel2.add(btnHungup);

		panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.EAST);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		textAreaDisplay = new JTextArea();
		textAreaDisplay.setEditable(false);
		textAreaDisplay.setColumns(20);
		textAreaDisplay.setRows(8);
		panel_2.add(textAreaDisplay);

		send = false;
		setVisible(true);
	}

	private class KeysListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			connection.dial(e.getActionCommand());
		}
	}

	@Override
	public void run(Connection c) {
		connection = c;
	}	

	@Override
	public void speak(String output) {
		textAreaDisplay.setText("");
		textOutput.setText(output);
	}

	@Override
	public void showDisplay(String output) {
		textAreaDisplay.setText(output);		
	}

}
